<?php

class Users_Model_Users {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Users_Model_DbTable_Users();
    }

    /**
     * 
     * @return type
     */
    public function fetchAll() {
        $select = $this->_dbtable->select();
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $data
     */
    public function addNewUser($data) {
        $this->_dbtable->insert($data);
    }

    /**
     * 
     * @param type $data
     * @param type $id
     */
    public function updateUser($data, $id) {
        $this->_dbtable->update($data, " id = $id");
    }

    /**
     * 
     * @param type $type
     * @return type
     * type = admin, user
     */
    public function getUserByType($type) {
        $select = $this->_dbtable->select();
        $select->where("user_type = ?", $type);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $email
     * @return type
     */
    public function getUserByEmail($email) {
        $select = $this->_dbtable->select();
        $select->where("email = ?", $email);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $type
     * @return type = user,admin
     */
    public function fetchAllUserByType($type) {
        $select = $this->_dbtable->select();
        $select->where("user_type = ?", $type);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $id
     * @return type
     */
    public function getUserById($id) {
        $select = $this->_dbtable->select();
        $select->where("id = ?", $id);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $detailsJson
     * @return type
     */
    public function getUserDetailsDecoded($detailsJson) {
        $sendD = array();
        $sendD['contact1'] = '';
        $sendD['contact2'] = '';
        $sendD['address'] = '';
        if ($detailsJson != null) {
            $djson = json_decode($detailsJson);
            $details = $djson->details;
            $contactJson = json_decode($details[0]);
            $sendD['contact1'] = $contactJson->contact->primary_contact;
            $sendD['contact2'] = $contactJson->contact->secondary_contact;
            $addJson = json_decode($details[1]);
            $sendD['address'] = $addJson->address;
        }
        return $sendD;
    }

    /**
     * 
     * @param type $id
     */
    public function getAllCommentsByUser($id) {
        $db = new Users_Model_Comments();
        return $db->getAllCommentByUser($id);
    }
    
}

